# 房贷计算器
def calculate_combined_mortgage(principal_g, principal_c, annual_interest_rate_g, annual_interest_rate_c, years):
    monthly_interest_rate_g = annual_interest_rate_g / 12 / 100  # 公积金贷款月利率
    monthly_interest_rate_c = annual_interest_rate_c / 12 / 100  # 商业贷款月利率
    payments = years * 12

    # 等额本息还款法
    def equal_amortization(principal, monthly_interest_rate):
        if monthly_interest_rate == 0:
            return principal / payments
        numerator = principal * monthly_interest_rate * (1 + monthly_interest_rate) ** payments
        denominator = (1 + monthly_interest_rate) ** payments - 1
        monthly_payment = numerator / denominator if denominator != 0 else 0
        return monthly_payment

    # 等额本金还款法
    def principal_equal_installments(principal, monthly_interest_rate):
        monthly_principal = principal / payments
        total_monthly_payments = []
        remaining_principal = principal
        for _ in range(payments):
            monthly_interest = remaining_principal * monthly_interest_rate
            remaining_principal -= monthly_principal
            total_monthly_payment = monthly_principal + monthly_interest
            total_monthly_payments.append(total_monthly_payment)
        return total_monthly_payments

    # 分别计算公积金贷款和商业贷款的每月还款额
    equal_amortization_payment_g = equal_amortization(principal_g, monthly_interest_rate_g)
    equal_amortization_payment_c = equal_amortization(principal_c, monthly_interest_rate_c)

    principal_equal_installments_payments_g = principal_equal_installments(principal_g, monthly_interest_rate_g)
    principal_equal_installments_payments_c = principal_equal_installments(principal_c, monthly_interest_rate_c)

    # 输出结果
    print(f"等额本息: 每月公积金贷款还款 {equal_amortization_payment_g:.2f} 元, 商业贷款还款 {equal_amortization_payment_c:.2f} 元")
    print("等额本金: 每月还款金额逐月减少，具体如下：")
    for i in range(payments):
        payment_g = principal_equal_installments_payments_g[i] if i < len(principal_equal_installments_payments_g) else 0
        payment_c = principal_equal_installments_payments_c[i] if i < len(principal_equal_installments_payments_c) else 0
        print(f"第 {i+1} 个月: 公积金贷款 {payment_g:.2f} 元, 商业贷款 {payment_c:.2f} 元")

# 使用示例
principal_g = 750000  # 公积金贷款本金
principal_c = 230000  # 商业贷款本金
annual_interest_rate_g = 2.85  # 公积金贷款年利率
annual_interest_rate_c = 3.2  # 商业贷款年利率
years = 30  # 贷款年限

calculate_combined_mortgage(principal_g, principal_c, annual_interest_rate_g, annual_interest_rate_c, years)
